package com.fanhao.dllo.soundfm.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.fanhao.dllo.soundfm.R;
import com.fanhao.dllo.soundfm.entity.RecommendResult;
import com.fanhao.dllo.soundfm.fragment.BaseFragment;
import com.fanhao.dllo.soundfm.tools.MyOnclickListener;
import com.fanhao.dllo.soundfm.volley.StringRequestUtf;
import com.fanhao.dllo.soundfm.volley.VolleySingleton;

import java.util.List;

import static com.fanhao.dllo.soundfm.R.id.relativeFirst_recommend_special;

/**
 * Created by dllo on 16/1/12.
 */
public class RecommendAdapter extends RecyclerView.Adapter
        <RecyclerView.ViewHolder> {
    private Context context;
    private ImageLoader imageLoader;
    private List<RecommendResult.EditorRecommendAlbumsEntity.ListEntity> editorList;
    private RecommendResult.HotRecommendsEntity hot;
    //    private List<RecommendResult.BulletAreaEntity.ListEntity> bulletList;
    private RecommendResult.SpecialColumnEntity special;
    private RecommendResult.DiscoveryColumnsEntity discovery;
    private LayoutInflater inflater;
    private MyOnclickListener mylistener;

    public void setMyOnclickListener(MyOnclickListener mylistener) {
        this.mylistener = mylistener;
    }

    public RecommendAdapter(Context context) {
        this.context = context;
        imageLoader = VolleySingleton.getInstance().getImageLoader();
    }

    // 获取editorList集合的方法
    public void addEditorData(List<RecommendResult.EditorRecommendAlbumsEntity.ListEntity> editorList) {
        this.editorList = editorList;
        notifyDataSetChanged();
    }

    // 获取hot的方法
    public void addHotData(RecommendResult.HotRecommendsEntity hot) {
        this.hot = hot;
        notifyDataSetChanged();
    }


    // 获取special方法
    public void addSpecialData(RecommendResult.SpecialColumnEntity special) {
        this.special = special;
        notifyDataSetChanged();
    }

    // 获取discovery方法
    public void addDiscoveryData(RecommendResult.DiscoveryColumnsEntity discovery) {
        this.discovery = discovery;
        notifyDataSetChanged();
    }


    /**
     * 在该刚方法里对viewType进行判断,来绑定不同布局
     * 详见 getItemViewType()方法
     *
     * @param parent
     * @param viewType
     * @return
     */
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        inflater = LayoutInflater.from(parent.getContext());
        if (viewType == 0) {
            return new LikesEditorViewHolder(inflater
                    .inflate(R.layout.recommend_adapter_item_likeseditor, parent, false));
        }
        if (viewType == 1) {
            return new SpecialViewHolder(inflater
                    .inflate(R.layout.recommend_adapter_item_special, parent, false));
        }
        if (viewType == 2) {
            return new DiscoveryViewHolder(inflater
                    .inflate(R.layout.recommend_adapter_item_discovery, parent, false));
        }
        if (viewType == 3) {
            {
                return new HotViewHolder(inflater
                        .inflate(R.layout.recommend_adapter_item_likeseditor, parent, false));
            }
        }

        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        // 如果是类似于小编推荐的界面
        if (holder instanceof LikesEditorViewHolder) {

            LikesEditorViewHolder likeHolder = (LikesEditorViewHolder) holder;
            // 当position是0时,加载小编推荐 为了好写TitleTv
            if (editorList != null && editorList.size() > 0) {
                likeHolder.TitleTv.setText("小编推荐");
                likeHolder.firstSubTitle.setText(editorList.get(0).getTrackTitle());
                likeHolder.firstPlays.setText(editorList.get(0).getTitle());
                likeHolder.firstIv.setImageUrl(editorList.get(0).getCoverLarge(), BaseFragment.getImageLoader());
                likeHolder.secondSubTitle.setText(editorList.get(1).getTrackTitle());
                likeHolder.secondPlays.setText(editorList.get(1).getTitle());
                likeHolder.secondIv.setImageUrl(editorList.get(1).getCoverLarge(), BaseFragment.getImageLoader());
                likeHolder.thirdSubTitle.setText(editorList.get(2).getTrackTitle());
                likeHolder.thirdPlays.setText(editorList.get(2).getTitle());
                likeHolder.thirdIv.setImageUrl(editorList.get(2).getCoverLarge(), BaseFragment.getImageLoader());
            }
        }

        // 如果是 精品听单的界面
        if (holder instanceof SpecialViewHolder) {
            SpecialViewHolder sHolder = (SpecialViewHolder) holder;
            if (special != null) {
                List<RecommendResult.SpecialColumnEntity.ListEntity> specialList = special.getList();
                if (specialList != null && specialList.size() > 0) {
                    sHolder.TitleTv.setText(special.getTitle());
                    sHolder.firstSubTitle.setText(specialList.get(0).getTitle());
                    sHolder.firstDescrip.setText(specialList.get(0).getSubtitle());
                    sHolder.firstCount.setText(specialList.get(0).getFootnote());
                    sHolder.firstIv.setImageUrl(specialList.get(0).getCoverPath(), BaseFragment.getImageLoader());
                    sHolder.secondSubTitle.setText(specialList.get(1).getTitle());
                    sHolder.secondDescrip.setText(specialList.get(1).getSubtitle());
                    sHolder.secondCount.setText(specialList.get(1).getFootnote());
                    sHolder.secondIv.setImageUrl(specialList.get(1).getCoverPath(), BaseFragment.getImageLoader());

                }
            }
        }
        // 如果是发现新奇的界面
        if (holder instanceof DiscoveryViewHolder) {
            DiscoveryViewHolder dHolder = (DiscoveryViewHolder) holder;
            if (discovery != null) {
                List<RecommendResult.DiscoveryColumnsEntity.ListEntity> discoveryList = discovery.getList();
                if (discoveryList != null && discoveryList.size() > 0) {
                    dHolder.TitleTv.setText(discovery.getTitle());
                    dHolder.firstSubTitle.setText(discoveryList.get(0).getTitle());
                    dHolder.firstDescrip.setText(discoveryList.get(0).getSubtitle());
                    dHolder.secondSubTitle.setText(discoveryList.get(1).getTitle());
                    dHolder.secondDescrip.setText(discoveryList.get(1).getSubtitle());
                    dHolder.thirdSubTitle.setText(discoveryList.get(2).getTitle());
                    dHolder.thirdDescrip.setText(discoveryList.get(2).getSubtitle());
                    dHolder.forthSubTitle.setText(discoveryList.get(3).getTitle());
                    dHolder.forthDescrip.setText(discoveryList.get(3).getSubtitle());
                }
            }
        }
        // 如果是听***的界面
        if (holder instanceof HotViewHolder) {
            HotViewHolder hHolder = (HotViewHolder) holder;
            if (hot != null) {
                // 获得第一个list对象
                List<RecommendResult.HotRecommendsEntity.ListEntity> hotList = hot.getList();
                hHolder.TitleTv.setText(hotList.get(position - 3).getTitle());

                if (hotList != null && hotList.size() > 0) {

                    List<RecommendResult.HotRecommendsEntity.ListEntity.ListEntityList> hotListEntity =
                            hotList.get(position - 3).getListEntList();
                    if (hotListEntity != null && hotListEntity.size() > 0) {
                        hHolder.firstSubTitle.setText(hotListEntity.get(0).getTrackTitle());
                        hHolder.firstPlays.setText(hotListEntity.get(0).getTitle());
                        hHolder.firstIv.setImageUrl(hotListEntity.get(0).getCoverLarge(), BaseFragment.getImageLoader());
                        hHolder.secondSubTitle.setText(hotListEntity.get(1).getTrackTitle());
                        hHolder.secondPlays.setText(hotListEntity.get(1).getTitle());
                        hHolder.secondIv.setImageUrl(hotListEntity.get(1).getCoverLarge(), BaseFragment.getImageLoader());
                        hHolder.thirdSubTitle.setText(hotListEntity.get(2).getTrackTitle());
                        hHolder.thirdPlays.setText(hotListEntity.get(2).getTitle());
                        hHolder.thirdIv.setImageUrl(hotListEntity.get(2).getCoverLarge(), BaseFragment.getImageLoader());
                        hHolder.itemPos = position;
                    }
                }

            }

        }


    }


    @Override
    public int getItemCount() {
        return hot != null && hot.getList().size() > 0 ?
                hot.getList().size() + 3 : 0;
    }

    /**
     * 该方法返回的是onCreateViewHolder 里的 viewType参数
     * 在onCreateViewHolder里通过判断viewType来进行绑定不同布局
     * 解释如下:
     * <p/>
     * 当 position == 0 || position == 1 || position >3 时,
     * 返回给 onCreateViewHolder 的 viewType 参数是 0
     * 当 position == 2 时,
     * 返回给 onCreateViewHolder 的 viewType 参数是 1
     * 当 position == 3 时,
     * 返回给 onCreateViewHolder 的 viewType 参数是 2
     * <p/>
     * 在onCreateViewHolder 里对 viewType 进行判断,
     * 当 viewType == 0 时,
     * 加载布局 likesEditorViewHolder;
     * 当 viewType == 1 时,
     * 加载布局 SpecialViewHolder;
     * 当 viewType == 2 时,
     * 加载布局 discoveryViewHolder;
     *
     * @param position
     * @return
     */
    @Override
    public int getItemViewType(int position) {
        if (position == 0) return 0;
        if (position == 1) return 1;
        if (position == 2) return 2;
        if (position > 2) return 3;
        return position;
    }


    // 内部类: 如同小编推荐
    class LikesEditorViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        private TextView TitleTv, firstSubTitle, firstPlays, secondSubTitle,
                secondPlays, thirdSubTitle, thirdPlays;
        private NetworkImageView firstIv, secondIv, thirdIv;
        private LinearLayout linearFirstRecommend, linearSecondRecommend, linearThirdRecommend;

        public LikesEditorViewHolder(View itemView) {
            super(itemView);
            TitleTv = (TextView) itemView.findViewById(R.id.tv_likesEditorTitle);
            firstSubTitle = (TextView) itemView.findViewById(R.id.tvFirst_likesEditorSubTitle);
            firstPlays = (TextView) itemView.findViewById(R.id.tvFirst_likesEditorPlays);
            secondSubTitle = (TextView) itemView.findViewById(R.id.tvSecond_likesEditorSubTitle);
            secondPlays = (TextView) itemView.findViewById(R.id.tvSecond_likesEditorPlays);
            thirdSubTitle = (TextView) itemView.findViewById(R.id.tvThird_likesEditorSubTitle);
            thirdPlays = (TextView) itemView.findViewById(R.id.tvThird_likesEditorPlays);
            firstIv = (NetworkImageView) itemView.findViewById(R.id.ivFirst_likesEditor);
            secondIv = (NetworkImageView) itemView.findViewById(R.id.ivSecond_likesEditor);
            thirdIv = (NetworkImageView) itemView.findViewById(R.id.ivThree_likesEditor);
            linearFirstRecommend = (LinearLayout) itemView.findViewById(R.id.linearFirst_likesEditor);
            linearSecondRecommend = (LinearLayout) itemView.findViewById(R.id.linearSecond_likesEditor);
            linearThirdRecommend = (LinearLayout) itemView.findViewById(R.id.linearThird_likesEditor);

            linearFirstRecommend.setOnClickListener(this);
            linearSecondRecommend.setOnClickListener(this);
            linearThirdRecommend.setOnClickListener(this);

        }

        @Override
        public void onClick(View v) {
            if (editorList != null && editorList.size() > 0) {
                switch (v.getId()) {
                    case R.id.linearFirst_likesEditor:
                        // 在RecommendFragment中判断第二个参数来区分跳到哪个界面
                        mylistener.myListener(editorList.get(0).getAlbumId(), 0);
                        break;
                    case R.id.linearSecond_likesEditor:
                        mylistener.myListener(editorList.get(1).getAlbumId(), 0);
                        break;
                    case R.id.linearThird_likesEditor:
                        mylistener.myListener(editorList.get(2).getAlbumId(), 0);
                        break;
                }
            }
        }
    }

    // 内部类: 听***界面
    class HotViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        private TextView TitleTv, firstSubTitle, firstPlays, secondSubTitle,
                secondPlays, thirdSubTitle, thirdPlays;
        private NetworkImageView firstIv, secondIv, thirdIv;
        private LinearLayout linearFirstRecommend, linearSecondRecommend, linearThirdRecommend;
        private int itemPos;

        public HotViewHolder(View itemView) {
            super(itemView);
            TitleTv = (TextView) itemView.findViewById(R.id.tv_likesEditorTitle);
            firstSubTitle = (TextView) itemView.findViewById(R.id.tvFirst_likesEditorSubTitle);
            firstPlays = (TextView) itemView.findViewById(R.id.tvFirst_likesEditorPlays);
            secondSubTitle = (TextView) itemView.findViewById(R.id.tvSecond_likesEditorSubTitle);
            secondPlays = (TextView) itemView.findViewById(R.id.tvSecond_likesEditorPlays);
            thirdSubTitle = (TextView) itemView.findViewById(R.id.tvThird_likesEditorSubTitle);
            thirdPlays = (TextView) itemView.findViewById(R.id.tvThird_likesEditorPlays);
            firstIv = (NetworkImageView) itemView.findViewById(R.id.ivFirst_likesEditor);
            secondIv = (NetworkImageView) itemView.findViewById(R.id.ivSecond_likesEditor);
            thirdIv = (NetworkImageView) itemView.findViewById(R.id.ivThree_likesEditor);
            linearFirstRecommend = (LinearLayout) itemView.findViewById(R.id.linearFirst_likesEditor);
            linearSecondRecommend = (LinearLayout) itemView.findViewById(R.id.linearSecond_likesEditor);
            linearThirdRecommend = (LinearLayout) itemView.findViewById(R.id.linearThird_likesEditor);

            linearFirstRecommend.setOnClickListener(this);
            linearSecondRecommend.setOnClickListener(this);
            linearThirdRecommend.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.linearFirst_likesEditor:
                    mylistener.myListener(
                            hot.getList().get(itemPos - 3).getListEntList().get(0).getAlbumId(), 1);
                    break;
                case R.id.linearSecond_likesEditor:
                    mylistener.myListener(
                            hot.getList().get(itemPos - 3).getListEntList().get(1).getAlbumId(), 1);
                    break;
                case R.id.linearThird_likesEditor:
                    mylistener.myListener(
                            hot.getList().get(itemPos - 3).getListEntList().get(2).getAlbumId(), 1);
                    break;
            }

        }
    }

    // 内部类: 精品听单
    class SpecialViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        private TextView TitleTv, firstSubTitle, firstDescrip, firstCount,
                secondSubTitle, secondDescrip, secondCount;
        private ImageView ivMore;
        private NetworkImageView firstIv, secondIv;
        private RelativeLayout relativeFirstSpecial, relativeSecondSpecial;

        public SpecialViewHolder(View itemView) {
            super(itemView);
            TitleTv = (TextView) itemView.findViewById(R.id.tv_specialTitle);
            firstSubTitle = (TextView) itemView.findViewById(R.id.tvFirst_specialSubTitle);
            firstDescrip = (TextView) itemView.findViewById(R.id.tvFirst_specialDescrip);
            firstCount = (TextView) itemView.findViewById(R.id.tvFirst_specialCount);
            secondSubTitle = (TextView) itemView.findViewById(R.id.tvSecond_specialSubTitle);
            secondDescrip = (TextView) itemView.findViewById(R.id.tvSecond_specialDescrip);
            secondCount = (TextView) itemView.findViewById(R.id.tvSecond_specialCount);
            ivMore = (ImageView) itemView.findViewById(R.id.iv_specialMore);
            firstIv = (NetworkImageView) itemView.findViewById(R.id.ivFirst_special);
            secondIv = (NetworkImageView) itemView.findViewById(R.id.ivSecond_special);
            relativeFirstSpecial = (RelativeLayout) itemView.findViewById(relativeFirst_recommend_special);
            relativeSecondSpecial = (RelativeLayout) itemView.findViewById(R.id.relativeSecond_recommend_special);

            relativeFirstSpecial.setOnClickListener(this);
            relativeSecondSpecial.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if (special.getList() != null) {
                switch (v.getId()) {
                    case R.id.relativeFirst_recommend_special:
                        mylistener.myListener(special.getList().get(0).getSpecialId(), 2);
                        break;
                    case R.id.relativeSecond_recommend_special:
                        mylistener.myListener(special.getList().get(1).getSpecialId(), 2);
                        break;
                }
            }

        }
    }

    // 内部类: 发现新奇
    class DiscoveryViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        private TextView TitleTv, firstSubTitle, firstDescrip,
                secondSubTitle, secondDescrip, thirdSubTitle, thirdDescrip,
                forthSubTitle, forthDescrip;
        private ImageView firstIv, secondIv, thirdIv, forthIv;
        private RelativeLayout relativeFirst, relativeSecond, relativeThird, relativeFouth;

        public DiscoveryViewHolder(View itemView) {
            super(itemView);
            TitleTv = (TextView) itemView.findViewById(R.id.tv_discovery);
            firstSubTitle = (TextView) itemView.findViewById(R.id.tvFirst_discoverySubTitle);
            firstDescrip = (TextView) itemView.findViewById(R.id.tvFirst_discoveryDescrip);
            secondSubTitle = (TextView) itemView.findViewById(R.id.tvSecond_discoverySubTitle);
            secondDescrip = (TextView) itemView.findViewById(R.id.tvSecond_discoveryDescrip);
            thirdSubTitle = (TextView) itemView.findViewById(R.id.tvThird_discoverySubTitle);
            thirdDescrip = (TextView) itemView.findViewById(R.id.tvThird_discoveryDescrip);
            forthSubTitle = (TextView) itemView.findViewById(R.id.tvForth_discoverySubTitle);
            forthDescrip = (TextView) itemView.findViewById(R.id.tvForth_discoveryDescrip);
            firstIv = (ImageView) itemView.findViewById(R.id.ivFirst_discovery);
            secondIv = (ImageView) itemView.findViewById(R.id.ivSecond_discovery);
            thirdIv = (ImageView) itemView.findViewById(R.id.ivThird_discovery);
            forthIv = (ImageView) itemView.findViewById(R.id.ivForth_discovery);
            relativeFirst = (RelativeLayout) itemView.findViewById(R.id.relativeFirst_discoveryAdapter);
            relativeSecond = (RelativeLayout) itemView.findViewById(R.id.relativeSecond_discoveryAdapter);
            relativeThird = (RelativeLayout) itemView.findViewById(R.id.relativeThird_discoveryAdapter);
            relativeFouth = (RelativeLayout) itemView.findViewById(R.id.relativeFouth_discoveryAdapter);

            relativeFirst.setOnClickListener(this);
            relativeSecond.setOnClickListener(this);
            relativeThird.setOnClickListener(this);
            relativeFouth.setOnClickListener(this);

        }

        @Override
        public void onClick(View v) {
            List<RecommendResult.DiscoveryColumnsEntity.ListEntity> discoveryList = discovery.getList();
            if (discoveryList != null && discoveryList.size() > 0) {
                switch (v.getId()) {
                    case R.id.relativeFirst_discoveryAdapter:
                        mylistener.myNoListener(discoveryList.get(0).getTitle(), 0);

                        break;
                    case R.id.relativeSecond_discoveryAdapter:

                        break;
                    case R.id.relativeThird_discoveryAdapter:

                        break;
                    case R.id.relativeFouth_discoveryAdapter:

                        break;
                }
            }

        }
    }


}
